package ansi.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import ansi.exception.DAOExcepcion;
import ansi.modelo.Reserva;
import ansi.util.ConexionBD;

public class ReservaDAO  extends BaseDAO {
	
	public Reserva obtener(int idReserva) throws DAOExcepcion {
		Reserva vo = new Reserva();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			String query = "SELECT idReserva, Estado_Reserva, Hora_Inicio, ";
			query += "Hora_Fin, Fecha_Reserva, Residente_DNI_Residente, ";
			query += "Area_Comun_idArea_Comun FROM Reserva WHERE idReserva = ? ";
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setInt(1, idReserva);
			rs = stmt.executeQuery();
			if (rs.next()) {
				vo.setIdReserva(rs.getInt("idReserva"));
				vo.setEstado_Reserva(rs.getString("Estado_Reserva"));
				vo.setHora_Inicio(rs.getInt("Hora_Inicio"));
				vo.setHora_Fin(rs.getInt("Hora_Fin"));
				vo.setFecha_Reserva(rs.getDate("Fecha_Reserva"));
				vo.setResidente_DNI_Residente(rs.getInt("Residente_DNI_Residente"));
				vo.setArea_Comun_idArea_Comun(rs.getInt("Area_Comun_idArea_Comun"));
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return vo;
	}
	
	public Collection<Reserva> listar() throws DAOExcepcion {
		Collection<Reserva> c = new ArrayList<Reserva>();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = ConexionBD.obtenerConexion();
			String query = "SELECT idReserva, Estado_Reserva, Hora_Inicio, ";
			query += "Hora_Fin, Fecha_Reserva, Residente_DNI_Residente, ";
			query += "Area_Comun_idArea_Comun FROM Reserva ORDER BY Fecha_Reserva DESC ";
			stmt = con.prepareStatement(query);
			rs = stmt.executeQuery();
			while (rs.next()) {
				Reserva vo = new Reserva();
				vo.setIdReserva(rs.getInt("idReserva"));
				vo.setEstado_Reserva(rs.getString("Estado_Reserva"));
				vo.setHora_Inicio(rs.getInt("Hora_Inicio"));
				vo.setHora_Fin(rs.getInt("Hora_Fin"));
				vo.setFecha_Reserva(rs.getDate("Fecha_Reserva"));
				vo.setResidente_DNI_Residente(rs.getInt("Residente_DNI_Residente"));
				vo.setArea_Comun_idArea_Comun(rs.getInt("Area_Comun_idArea_Comun"));
				c.add(vo);
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return c;
	}
	
	public void insertar(Reserva vo) throws DAOExcepcion {
		String query = "INSERT INTO Reserva ";
		query += "(Estado_Reserva, Hora_Inicio, ";
		query += "Hora_Fin, Fecha_Reserva, Residente_DNI_Residente, Area_Comun_idArea_Comun) ";
		query += "VALUES (?, ?, ?, ?, ?, ?)";
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setString(1, vo.getEstado_Reserva());
			stmt.setInt(2, vo.getHora_Inicio());
			stmt.setInt(3, vo.getHora_Fin());
			stmt.setDate(4, new java.sql.Date(vo.getFecha_Reserva().getTime()));
			stmt.setInt(5, vo.getResidente_DNI_Residente());
			stmt.setInt(6, vo.getArea_Comun_idArea_Comun());
			int i = stmt.executeUpdate();
			if (i != 1) {
				throw new SQLException("No se pudo insertar");
			}
			
			// Obtener el ultimo id
			int id = 0;
			query = "select last_insert_id()";
			stmt = con.prepareStatement(query);
			rs = stmt.executeQuery();
			if (rs.next()) {
				id = rs.getInt(1);
			}
			vo.setIdReserva(id);
			
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
	}
	
	public Reserva actualizar(Reserva vo) throws DAOExcepcion {
		String query = "UPDATE Reserva SET ";
		query += "Estado_Reserva = ?, Hora_Inicio = ?, ";
		query += "Hora_Fin = ?, Fecha_Reserva = ?, Residente_DNI_Residente = ?, Area_Comun_idArea_Comun = ? ";
		query += "WHERE idReserva = ? ";
		Connection con = null;
		PreparedStatement stmt = null;
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setString(1, vo.getEstado_Reserva());
			stmt.setInt(2, vo.getHora_Inicio());
			stmt.setInt(3, vo.getHora_Fin());
			stmt.setDate(4, new java.sql.Date(vo.getFecha_Reserva().getTime()));
			stmt.setInt(5, vo.getResidente_DNI_Residente());
			stmt.setInt(6, vo.getArea_Comun_idArea_Comun());
			stmt.setInt(7, vo.getIdReserva());
			int i = stmt.executeUpdate();
			if (i != 1) {
				throw new SQLException("No se pudo actualizar");
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return vo;
	}
	
	public void eliminar(int idReserva) throws DAOExcepcion {
		String query = "DELETE FROM Reserva WHERE idReserva = ?";
		Connection con = null;
		PreparedStatement stmt = null;
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setInt(1, idReserva);
			int i = stmt.executeUpdate();
			if (i != 1) {
				throw new SQLException("No se pudo eliminar");
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
	}
	
}
